<?
/** @file
 * @author Andrzej Mararok Wasiak (mararok@gmail.com)
 */
 
/** Wyrzucany gdy są jakieś nie oczekiwane błędy w parametrach akcji.
 */
class ActionParamException extends Exception {
   protected $ParamName;
   function __construct($paramName) {
      $this->ParamName = $paramName;
   }
}

/** Wyrzucany gry parametr ma nie prawidłowy typ.
 */
class ActionParamInvalidTypeException extends ActionParamException {
   function getMessage() {
      return "Invalid param type: $this->ParamName";
   }
}
/** Wyrzucany gdy parametr o podanej nazwie nie istnieje.
 */
class ActionParamNotExistsException extends ActionParamException {
   function getMessage() {
      return "Param not exists: $this->ParamName";
   }
}

/** Obsługuje parametry dla akcji.
 */
class ActionParams {
   private $Params;
   
   /** 
    * @param (array) $params Tablica z parametrami akcji.
    */
   function __construct($params) {
      $this->Params = $params;
   }
   
   private function get($paramName) {
      if (array_key_exists($paramName, $this->Params))
         return $Params[$paramName];
      throw new ActionParamNotExistsException($paramName); 
   }
   
   /** Returned param as int;
    * @return (int)
    */
   function getInt($paramName) {
      $param = $this->get($paramName);
      if (is_numeric($param)) 
         return (int)$param;
      throw new ActionParamInvalidTypeException($paramName);
   }
   
   /** Returned param as float;
    * @return (float)
    */
   function getFloat($param_name) {
      $param = $this->get($param_name);
      if (is_numeric($param))
         return (float)$param;
       throw new ActionParamInvalidTypeException($paramName);
   }
   
   /** Returned param as bool;
    * @return (bool)
    */
   function getBool($paramName) {
      $param = $this->get($param_name);
      if (is_bool($param)) 
         return (bool)$param;
       throw new ActionParamInvalidTypeException($paramName);
   }
  
   /** Returned param as string, protected from sql-injection;
    * @return (string)
    */
   function getString($param_name) {
      return DB::safeString($this->getUnsafeString($param_name));
   }
   
   /** Returned param as string - not safe 
    * @return (string)
    */
   function getUnsafeString($param_name) {
      return (string)$this->get($param_name);
   }
    
}

?>